local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

local MenuUtils = require 'scripts.utils.MenuUtils'
local menuUtils = MenuUtils.new()

function scene:showBackButton()
   local group=self.view

   local backToMnBtn = menuUtils:createButton(X*0.5, Y*0.9, "MENU", function ()
      storyboard.gotoScene("scripts.scenes.menu",{effect = "fade",time = 100})
      return true
   end)
   group:insert( backToMnBtn )
end

function scene:scoreForm(points)
   local group = self.view

   local nameLabel = display.newText({text= "NAME", x=X*0.2, y=Y*0.5, font=Font.get('seagram'), fontSize=20} )

   local playerName = native.newTextField( X*0.6, Y*0.5, X*0.5, Y*0.2 )
   playerName.align = "center"
   playerName.size = 32
   playerName:setTextColor( 1, 0.5, 0 )

   local function networkListener( event )
      if ( event.isError ) then
         print( "Network error!")
      else
         print ( "RESPONSE: " .. event.response )
      end
   end

   local continueBtn = menuUtils:createButton(X*0.5, Y*0.7, "UPLOAD SCORE", function (event)
      if(playerName.text ~= "") then
         network.request( "http://www.vudduu.com/batmggj/saveScore.php?name=" .. playerName.text .. "&points=" .. points, "GET", networkListener)

         event.target:setEnabled(false)
         event.target.isVisible = false
         nameLabel:removeSelf()
         playerName:removeSelf()
      else
         local function onComplete( event )
            local action = event.action
            if "clicked" == event.action then
            end
         end
         local alert = native.showAlert( "Please", "Fill the name with a name", { "OK" }, onComplete )
      end
      return true
   end)

   group:insert(nameLabel)
   group:insert(playerName)
   group:insert(continueBtn)
end

function scene:showDeaths(msg)
   local group=self.view

   local finalText = display.newText( {text="Total Deaths", x=X*0.2, y=Y*0.2, font=Font.get('seagram'), fontSize=20} )
   local scoreText = display.newText( {text=msg, x=X*0.2+X*0.2, y=Y*0.2, font=Font.get('seagram'),fontSize= 20} )

   group:insert(finalText)
   group:insert(scoreText)
end

function scene:showScore(msg)
   local group=self.view

   local finalText = display.newText({text= "FINAL SCORE", x=X*0.5, y=Y*0.1, font=Font.get('seagram'), fontSize=20} )
   local scoreText = display.newText( {text=msg, x=X*0.5, y=Y*0.3, font=Font.get('seagram'), fontSize=40} )

   group:insert(finalText)
   group:insert(scoreText)
end

function scene:createScene(event)
   logger:gameEvent("Create Score Scene")
   storyboard.removeAll()
end

function scene:enterScene(event)
   logger:gameEvent("Entering Score Scene")

   local params = event.params
   scene:showScore(params.finalScore)
   scene:scoreForm(params.finalScore)
   --scene:showDeaths(_params.totalDeaths)
   scene:showBackButton()
end

function scene:exitScene(event)
   logger:gameEvent("exit Score Scene")
end

function scene:destroyScene(event)
   logger:gameEvent("destroy Score Scene")
end

scene:addEventListener("createScene",scene)
scene:addEventListener("enterScene",scene)
scene:addEventListener("exitScene",scene)
scene:addEventListener("destroyScene", scene)

return scene

